package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AppInfo;
import com.wude.bean.PinglunBean;
import com.wude.bean.ShareInfo;
import com.wude.bean.UserInfo;
import com.wude.guild.CommentActivity;
import com.wude.guild.LoginActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.GameDetListPinglunAdapter;
import com.wude.guild.dialog.SharedDialog;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 游戏详情内的评论Fragment
 * Created by Administrator on 2017/3/27.
 */
@SuppressLint("HandlerLeak")
public class GameDetCommentFragment extends Fragment {

    @BindView(R.id.shoucang)
    ImageView shoucang;
    @BindView(R.id.share)
    ImageView share;
    @BindView(R.id.pinglun)
    TextView pinglun;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.springview)
    SpringView springview;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    private GameDetListPinglunAdapter gameDetListPinglunAdapter;
    private int limit = 1;
    ArrayList<PinglunBean> gameInfos = new ArrayList<>();
    private String id;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_gamedet_comm, null);
        ButterKnife.bind(this, inflate);
        id = getArguments().getString("key");
        initview();
        initAndReflsh();
        return inflate;
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("GameDetCommentFragment");
    }

    private void initview() {
        gameDetListPinglunAdapter = new GameDetListPinglunAdapter(getActivity());
        listview.setAdapter(gameDetListPinglunAdapter);
        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
        springview.setHeader(new SimpleHeader(getActivity()));
        springview.setFooter(new SimpleFooter(getActivity()));
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh();
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
        }
    };

    private void initAndReflsh() {
        gameInfos.clear();
        limit = 1;
        Map<String, String> map = new HashMap<String, String>();
        map.put("p", limit + "");
        map.put("game_id", id);
        HttpCom.POST(handler, HttpCom.getGameDetPingLunUrl(), map, false);
    }

    private void onLoadMord() {
        limit = ++limit;
        Map<String, String> map = new HashMap<String, String>();
        map.put("p", limit + "");
        map.put("game_id", id);
        HttpCom.POST(mhandler, HttpCom.getGameDetPingLunUrl(), map, false);
    }

    @OnClick({R.id.shoucang, R.id.share, R.id.pinglun})
    public void onClick(View view) {
//        UserInfo loginUser = Utils.getLoginUser();
//        if (loginUser == null) {
//            ToastUtil.showToast("请先登录");
//            return;
//        }
        switch (view.getId()) {
            case R.id.shoucang:
                Collection();
                break;
            case R.id.share:
                Share();
                break;
            case R.id.pinglun:
                Pinglun();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        HashMap<String, String> map = new HashMap<>();
        map.put("game_id", id);
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            map.put("token", loginUser.token);
        }
        HttpCom.POST(qhandler, HttpCom.getGameDetUrl(), map, false);
        MobclickAgent.onPageStart("GameDetCommentFragment");
    }

    /**
     * 评论
     */
    private void Pinglun() {
        Intent intent = new Intent(getContext(), CommentActivity.class);
        intent.putExtra("gemeId", id);
        startActivityForResult(intent, 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && data != null) {
            boolean updata = data.getBooleanExtra("updata", false);
            if (updata) {//刷新评论
                initAndReflsh();
            }
        }
    }

    /**
     * 分享
     */
    private void Share() {
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("game_id", id + "");
            HttpCom.POST(khandler, HttpCom.getGameShareURL(), map, false);
        } else {
            getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }

    /**
     * 收藏
     */
    private void Collection() {
        UserInfo loginUser = Utils.getLoginUser();
        if (loginUser != null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("token", loginUser.token);
            map.put("game_id", id + "");
            if (appInfo != null) {
                switch (appInfo.Collection) {
                    case 1:
                        map.put("status", "2");
                        break;
                    case 2:
                        map.put("status", "1");
                        break;
                }
                HttpCom.POST(xhandler, HttpCom.getShouCangUrl(), map, false);
            }
        } else {
            getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }


    /**
     * 刚进来请求
     */

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<PinglunBean> pinglunBeen = HttpUtils.DNSPinglun(msg.obj.toString());
                    if (pinglunBeen != null) {
                        errorLayout.setVisibility(View.GONE);
                        springview.setVisibility(View.VISIBLE);
                        gameInfos.addAll(pinglunBeen);
                        gameDetListPinglunAdapter.setList(gameInfos);
                    } else {
                        springview.setVisibility(View.GONE);
                        errorLayout.setVisibility(View.VISIBLE);
                        errorText.setText("该游戏暂无评论");
                    }
                    break;
                case 2:
                    springview.setVisibility(View.GONE);
                    errorLayout.setVisibility(View.VISIBLE);
                    errorText.setText("网络异常");
                    break;
            }
        }
    };

    /**
     * 加载更多请求
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<PinglunBean> pinglunBeen = HttpUtils.DNSPinglun(msg.obj.toString());
                    if (pinglunBeen != null) {
                        gameInfos.addAll(pinglunBeen);
                        gameDetListPinglunAdapter.setList(gameInfos);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    break;
                case 2:
                    break;
            }
        }
    };

    /**
     * 发表游戏评论
     */
    Handler vhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean a = HttpUtils.DNSPiNGLUN(msg.obj.toString());
                    if (a) {
                        initAndReflsh();
                    }
                    break;
                case 2:

                    break;
            }
        }
    };
    private SharedDialog dialig;
    /**
     * 获取分享内容
     */
    Handler khandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ShareInfo bena = HttpUtils.DNSShare(msg.obj.toString());
                    if (bena != null) {
                        if (dialig == null) {
                            dialig = new SharedDialog(getContext(), bena, 2);
                            dialig.show();
                        } else if (dialig != null && !dialig.isShowing()) {
                            dialig.show();
                        }
                    } else {
                        ToastUtil.showToast("分享失败");
                    }
                    break;
                case 2:

                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 收藏
     */
    Handler xhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean b = HttpUtils.DNSShou(msg.obj.toString());
                    if (b) {
                        switch (appInfo.Collection) {
                            case 1:         //已收藏
                                appInfo.Collection = 2;
                                shoucang.setBackgroundResource(R.mipmap.game_btn_collection_nor);
                                ToastUtil.showToast("取消了收藏");
                                break;
                            case 2:         //未收藏
                                appInfo.Collection = 1;
                                shoucang.setBackgroundResource(R.mipmap.game_btn_collection_pre);
                                ToastUtil.showToast("已成功收藏");
                                break;
                        }
                    }
                    break;
                case 2:

                    break;
            }
        }
    };


    private AppInfo appInfo;
    /**
     * 游戏详情
     */
    Handler qhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    appInfo = HttpUtils.DNSGameDel(msg.obj.toString());
                    if (appInfo != null) {
                        switch (appInfo.Collection) {
                            case 1:         //已收藏
                                shoucang.setBackgroundResource(R.mipmap.game_btn_collection_pre);
                                break;
                            case 2:         //未收藏
                                shoucang.setBackgroundResource(R.mipmap.game_btn_collection_nor);
                                break;
                        }
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
